﻿package
{
    import com.purplemovies.display.ExtContentController;
    import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flash.display.Sprite;
	public class Main extends Sprite {
		
	// CONSTRUCTOR:
		function Main() {
			init();
		}
		
		
		private          var 
	//  PROPERTIES:
        _data_loader		:URLLoader,
		_content_loader		:Loader,
        _manifest           :XML,
        _init_controller    :ExtContentController;
		
		
	// METHODS:
		private function init():void {
			
			var tf:TextField = new TextField();
			tf.scaleX = tf.scaleY = 4;
			tf.selectable = false;
			tf.autoSize = 'left';
			tf.text = 'HIT ALT + F4 TO EXIT';
			addChild( tf );
			
			stage.displayState = StageDisplayState.FULL_SCREEN;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown );
			
		// Load System Data:
			_data_loader = new URLLoader();
			
			_data_loader.addEventListener(IOErrorEvent.IO_ERROR, 		onDataError);
			_data_loader.addEventListener(ProgressEvent.PROGRESS,		onDataProgress);
			_data_loader.addEventListener(Event.COMPLETE,				onDataComplete);
			
			_data_loader.load( new URLRequest('content/data.xml') );
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			//if ( e.keyCode == Keyboard.ESCAPE
			switch ( e.keyCode ) {
				case Keyboard.ESCAPE		: stage.displayState = StageDisplayState.FULL_SCREEN; break;
			}
		}
		
		private function onDataError(e:Event):void {
			// TODO log errors.
			trace('data ', e);
		}
		
		private function onDataProgress(e:Event):void {
			// TODO display progress.
		}
		
		private function onDataComplete(e:Event):void {
			_manifest = new XML( _data_loader.data );	// Throw data into XML structure.
			
		// LOAD INITIAL CONTENT
			_content_loader = new Loader();
			_content_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR,		onGameLoadError );
			_content_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, 	onGameLoadProgress );
			_content_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, 			onGameLoadComplete );
			
			_content_loader.load(  new URLRequest( _manifest.@init )  );
		}
		
		private function onGameLoadError(e:Event):void {
			// TODO log errors.
			trace(e);
		}
		
		private function onGameLoadProgress(e:Event):void {
			// TODO display progress.
		}
		
		private function onGameLoadComplete(e:Event):void {
			//stage.stageWidth = _content_loader.contentLoaderInfo.width;
			//var scale = (1 / _content_loader.contentLoaderInfo.width) * stage.stageHeight;
			//trace('added!!!!!!!!!!!! ', scale);
			
			var content:DisplayObject = _content_loader.contentLoaderInfo.content;
            /*
            for each( var k:* in _manifest.children ) {
                trace( 'arse ::', k );
            }
            */
            
            _init_controller = new ExtContentController( content, _content_loader, _manifest );
            
            content.data = _manifest;
            
			//game.scaleX = game.scaleY = scale;
			
			addChild( content );
		}
		
	}
}